package com.enlighten.coinDataFetcher;

import java.awt.Color;
import java.awt.image.BufferedImage;

public class CircleRadiusDensityCalculator {

	private BufferedImage bufferedImage;

	public CircleRadiusDensityCalculator(BufferedImage bufferedImage) {
		this.bufferedImage = bufferedImage;
	}

	public int getDensityForRadius(int radius){
		int imgWidth = bufferedImage.getWidth();
		int imgHeight = bufferedImage.getHeight();
		if ( radius % 2 == 0 )
			radius++;
		int squaredRadius = radius*radius;
		int blacks = 0;
		int whites = 0;
		for(int i=0; i < imgWidth ; i++){
			for(int j=0; j < imgHeight ; j++){
				if ( belongsToCircleCircunference(getSquaredDistance(i,j,radius), squaredRadius, radius)){
					if ( bufferedImage.getRGB(i, j) == Color.WHITE.getRGB() )
						whites++;
					else
						blacks++;
				}
					
			}
		}
		
		return whites;
	}

	private int getSquaredDistance(int i, int j, int radius) {
		int translatedX = i - radius;
		int translatedY = j - radius;
		return translatedX*translatedX + translatedY*translatedY;
	}

	private boolean belongsToCircleCircunference(int squaredDistance, int squaredRadius, int radius) {
		return (squaredDistance > squaredRadius - radius - 1) && (squaredDistance < squaredRadius + radius + 1);
	}
}
